package per.zhang.serverPrevioder.service.impl;

import lombok.extern.slf4j.Slf4j;
import per.zhang.serverPrevioder.base.page.PageList;
import per.zhang.serverPrevioder.base.service.impl.BaseServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import per.zhang.serverPrevioder.dto.respone.VaccineRespondDTO;
import per.zhang.serverPrevioder.entity.Appointment;
import per.zhang.serverPrevioder.entity.Vaccine;
import per.zhang.serverPrevioder.mapper.VaccineMapper;
import per.zhang.serverPrevioder.service.AppointmentService;
import per.zhang.serverPrevioder.service.VaccineOrderService;
import per.zhang.serverPrevioder.service.VaccineService;


import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zhang
 * @since 2021-01-25
 */
@Service
@Slf4j
public class VaccineServiceImpl extends BaseServiceImpl<VaccineMapper, Vaccine> implements VaccineService {


        @Autowired
        private VaccineMapper vaccineMapper;

        @Autowired
        private AppointmentService appointmentService;


    @Override
    public PageList<VaccineRespondDTO> seleceByUserId(String userId, Integer pageNum, Integer pageSize) throws Exception {
        Vaccine vaccine = new Vaccine();
        vaccine.setState("0");
        PageList<Vaccine> page = findPageData(vaccine,pageNum,pageSize);
        Appointment appointment = new Appointment();
        appointment.setUserId(userId);
        ArrayList<VaccineRespondDTO> data = new ArrayList<>();
        for (Vaccine vaccineItem : page.getData()) {
            if (vaccineItem.getAppointmentNum()<=0) {
                vaccineItem.setState("余量不足");
                data.add(new VaccineRespondDTO(vaccineItem));
                continue;
            }
            appointment.setVaccineId(vaccineItem.getVaccineId());
            List<Appointment> appointmentList = appointmentService.findByParams(appointment);
            if (appointmentList.isEmpty()) {
                vaccineItem.setState("可接种");
                data.add(new VaccineRespondDTO(vaccineItem));
                continue;
            }else {
                vaccineItem.setState("已接种");
                data.add(new VaccineRespondDTO(vaccineItem));
            }
        }
        PageList<VaccineRespondDTO> vaccineRespondDTOPageList = new PageList<>(data, page.getPageNum(), page.getPageSize(), page.getTotalItems());
        return vaccineRespondDTOPageList;
    }
}